package com.huitone.gddw.service.impl.system;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.Role;
import com.huitone.gddw.dao.system.IRoleDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.system.RoleDTO;
import com.huitone.gddw.service.system.IRoleMenuService;
import com.huitone.gddw.service.system.IRoleService;
import com.huitone.gddw.service.system.IUserRoleService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 角色管理 接口实现类
 * @author wuluodan
 *
 */
@Service
public class RoleService implements IRoleService {

	@Autowired
	private IRoleDao roleDao;
	
	@Autowired
	private IRoleMenuService roleMenuService;
	
	@Autowired
	private IUserRoleService userRoleService;
	
	@Override
	public QueryPage<RoleDTO> selectPage(QueryPage<Role> page) {
		Map<String, Object> map = page.getParams();
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<RoleDTO> result = roleDao.selectPage(map);
		int totalCount = roleDao.selectCount(map);
		
		QueryPage<RoleDTO> queryPage = new QueryPage<RoleDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}
	
	@Override
	public List<Role> getAll() {
		return roleDao.getAll();
	}

	@Override
	public Role getById(String roleId) {
		return roleDao.selectByPrimaryKey(roleId);
	}

	@Override
	public RoleDTO toUpdate(String roleId) {
		return roleDao.toUpdate(roleId);
	}

	@Override
	public void changeStatus(Role entity) {
		this.update(entity);
	}

	@Override
	public void save(RoleDTO roleDTO) {
		Role role = new Role();
		BeanUtils.copyProperties(roleDTO, role);
		this.save(role);
	}
	
	private void save(Role entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		roleDao.insertSelective(entity);
	}

	@Override
	public void update(RoleDTO roleDTO) {
		Role role = this.getById(roleDTO.getRoleId());
		role.setRoleName(roleDTO.getRoleName());
		role.setAcsFlag(roleDTO.getAcsFlag());
		role.setRemark(roleDTO.getRemark());
		this.update(role);
	}
	
	private void update(Role entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		roleDao.updateByPrimaryKey(entity);
	}

	@Override
	public void delete(String roleId) {
		userRoleService.delAllByRoleId(roleId);
		roleMenuService.delAllByRoleId(roleId);
		roleDao.deleteByPrimaryKey(roleId);
	}

	@Override
	public void changeMenusAndAcsFlag(String roleId, String menuIds, String acsFlag) {
		roleMenuService.changeRoleMenus(roleId, menuIds);
		
		// 设置数据访问权限
		if (StringUtils.isNotBlank(acsFlag)) {
			Role entity = this.getById(roleId);
			if (StringUtils.isNotBlank(entity.getAcsFlag()) && !entity.getAcsFlag().equals(acsFlag)) {
				entity.setAcsFlag(acsFlag);
				this.update(entity);
			}
		}
	}
	
	@Override
	public List<Role> getByUserId(String userId) {
		return roleDao.getByUserId(userId);
	}
	
	@Override
	public Boolean isAdmin(String userId) {
		List<Role> list = this.getByUserId(userId);
		for (Role role : list) {
			if (role.getRoleId().equals("admin")) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public String getHighestAcsFlag(String userId) {
		String acsFlag = "";
		List<Role> list = this.getByUserId(userId);
		for (Role role : list) {
			if (StringUtils.isNotBlank(role.getAcsFlag())) {
				if (StringUtils.isBlank(acsFlag)) {
					acsFlag = role.getAcsFlag();
				} else {
					if (acsFlag.compareTo(role.getAcsFlag()) < 0) {
						acsFlag = role.getAcsFlag();
					}
				}
			}
		}
		return acsFlag;
	}
	
	@Override
	public boolean isRootAcsFlag(String userId) {
		String acsFlag = this.getHighestAcsFlag(userId);
		if (StringUtils.isBlank(acsFlag)) {
			return false;
		}
		return acsFlag.equals(Role.ACS_ROOT);
	}

}
